<template>
	<view>
		<!-- 在线订购 -->
		<view class="buy-online">
			<view class="title">
				——&nbsp;&nbsp;在线订购&nbsp;&nbsp;——
			</view>
			<view class="content">
				<view class="block" v-for="(v,index) in page.list" :key="index">
					<image class="img" :src="v.img_url"></image>
					<text class="name">{{v.name}}</text>
					<view class="see">
						<text class="money">¥&nbsp;{{v.unit_price}}</text>
						<button class="btn" @tap="goDetail(v)">查看</button>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				page: {
					pageNumber: 1,
					firstPage: true,
					lastPage: true,
					list: [],
				},

				noMore: false,

				current: null,

			};
		},
		props: ['showNum'],
		onReachBottom(){
			console.log('触底');
			if(this.page.lastPage) {this.noMore = true;return;}
			this.getDataMore(++this.page.pageNumber);
		},
		methods:{
			getData(pageNumber){
				let self = this;
				uni.showLoading({title: '加载中',mask: true});
				this.config.post('/api/product/findPage', {
					pageNum: pageNumber?pageNumber:1,
				}).then(res=>{
					uni.hideLoading();
					if(res.data.code==='0'){
						self.page.pageNumber = res.data.page.pageNumber;
						self.page.firstPage = res.data.page.firstPage;
						self.page.lastPage = res.data.page.lastPage;
						self.page.list = res.data.page.list;
					}else{
						uni.showToast({title: self.config.handleError(res.data),icon: 'none'});
					}
				})
			},
			getDataMore(pageNumber){
				let self = this;
				uni.showLoading({title: '加载中',mask: true});
				this.config.post('/api/product/findPage', {
					pageNum: pageNumber?pageNumber:1,
				}).then(res=>{
					uni.hideLoading();
					if(res.data.code==='0'){
						self.page.pageNumber = res.data.page.pageNumber;
						self.page.firstPage = res.data.page.firstPage;
						self.page.lastPage = res.data.page.lastPage;
						self.page.list = self.page.list.concat(res.data.page.list);
					}else{
						uni.showToast({title: self.config.handleError(res.data),icon: 'none'});
					}
				})
			},
			goDetail(v){
				uni.navigateTo({
					url: '/pages/main/make-order/make-order?id='+v.id
				})
			},
			homeFun(){
				if(this.page.list.length) return;
				this.getData()
			},
		},
		mounted() {
			this.getData('1');
		},
	}
</script>

<style lang="less" scoped>
	.buy-online{
		width: 100%;
		margin-top: 10upx;
		background-color: #fff;
		text-align: center;
		.title{
			width: 100%;
			font-size: 25upx;
			font-weight: bold;
			color: #007aff;
			padding-top: 20upx;
		}
		.content{
			width: 100%;
			display: inline-flex;
			flex-wrap: wrap;
			margin-bottom: 10upx;
			.block{
				width: 46%;margin: 10upx 2%;
				height: 300upx;
				box-shadow: 0 0 10px 0 #ccc;
				border-radius: 8upx;
				margin-top: 20upx;
				text-align: left;
				.img{
					width: 100%;
					height: 200upx;
					border-top-left-radius: 8upx;
					border-top-right-radius: 8upx;
				}
				.name{
					font-size: 25upx;
					margin-left: 10upx;
				}
				.see{
					display: flex;
					.money{
						font-size: 22upx;
						margin-left: 10upx;
					}
					.btn{
						margin-right: 10upx;
						margin-top: -25upx;
						width: 100upx;
						height: 50upx;
						font-size: 20upx;
						color: #fff;
						background-color: #000;
					}
				}
			}
			
		}
	}
</style>
